// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

var TVKey = require('ToolUtil').TVKey;
cc.Class({
    extends: cc.Component,
    properties: {
        DefaultView: cc.Node,
        autoRegistEvent: false,
        backFun: cc.Component.EventHandler,
        menuFun: cc.Component.EventHandler,
        _focuseArr: null,
        _selectView: null,
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    clearFocuse() {
        for (var i = 0; i < this._focuseArr.length; i++) {
            this._focuseArr[i].clearFocuse();
        }
    },


    choseFocuse(direction) {
        if (this._focuseArr.length == 0)
            return

        var fix = 999999;
        var tmp = null;
        switch (direction) {
            case cc.KEY.up:
            case TVKey.up:
                {
                    for (var i = 0; i < this._focuseArr.length; i++) {
                        if (this._focuseArr[i] == this._selectView)
                            continue;
                        if (!this._focuseArr[i].isEnable())
                            continue;
                        if (this._focuseArr[i].node.y > this._selectView.node.y) {
                            var tmpd = cc.pDistance(this._focuseArr[i].node.getPosition(), this._selectView.node.getPosition());
                            if (tmpd < fix) {
                                fix = tmpd;
                                tmp = this._focuseArr[i];
                            }
                        }
                    }
                }
                break;
            case cc.KEY.down:
            case TVKey.down:
                {
                    for (var i = 0; i < this._focuseArr.length; i++) {
                        if (this._focuseArr[i] == this._selectView)
                            continue;
                        if (!this._focuseArr[i].isEnable())
                            continue;
                        if (this._focuseArr[i].node.y < this._selectView.node.y) {
                            var tmpd = cc.pDistance(this._focuseArr[i].node.getPosition(), this._selectView.node.getPosition());
                            if (tmpd < fix) {
                                fix = tmpd;
                                tmp = this._focuseArr[i];
                            }
                        }
                    }
                }
                break;
            case cc.KEY.left:
            case TVKey.left:
                {
                    for (var i = 0; i < this._focuseArr.length; i++) {
                        if (this._focuseArr[i] == this._selectView)
                            continue;
                        if (Math.abs(this._focuseArr[i].node.y - this._selectView.node.y) > 50)
                            continue;
                        if (!this._focuseArr[i].isEnable())
                            continue;
                        if (this._focuseArr[i].node.x < this._selectView.node.x) {
                            var tmpd = cc.pDistance(this._focuseArr[i].node.getPosition(), this._selectView.node.getPosition());
                            if (tmpd < fix) {
                                fix = tmpd;
                                tmp = this._focuseArr[i];
                            }
                        }
                    }
                    if (tmp == null) {
                        for (var i = 0; i < this._focuseArr.length; i++) {
                            if (this._focuseArr[i] == this._selectView)
                                continue;
                            if (!this._focuseArr[i].isEnable())
                                continue;
                            if (this._focuseArr[i].node.x < this._selectView.node.x) {
                                var tmpd = cc.pDistance(this._focuseArr[i].node.getPosition(), this._selectView.node.getPosition());
                                if (tmpd < fix) {
                                    fix = tmpd;
                                    tmp = this._focuseArr[i];
                                }
                            }
                        }
                    }
                }
                break;
            case cc.KEY.right:
            case TVKey.right:
                {
                    for (var i = 0; i < this._focuseArr.length; i++) {
                        if (this._focuseArr[i] == this._selectView)
                            continue;
                        if (Math.abs(this._focuseArr[i].node.y - this._selectView.node.y) > 50)
                            continue;
                        if (!this._focuseArr[i].isEnable())
                            continue;
                        if (this._focuseArr[i].node.x > this._selectView.node.x) {
                            var tmpd = cc.pDistance(this._focuseArr[i].node.getPosition(), this._selectView.node.getPosition());
                            if (tmpd < fix) {
                                fix = tmpd;
                                tmp = this._focuseArr[i];
                            }
                        }
                    }
                    if (tmp == null) {
                        for (var i = 0; i < this._focuseArr.length; i++) {
                            if (this._focuseArr[i] == this._selectView)
                                continue;
                            if (!this._focuseArr[i].isEnable())
                                continue;
                            if (this._focuseArr[i].node.x > this._selectView.node.x) {
                                var tmpd = cc.pDistance(this._focuseArr[i].node.getPosition(), this._selectView.node.getPosition());
                                if (tmpd < fix) {
                                    fix = tmpd;
                                    tmp = this._focuseArr[i];
                                }
                            }
                        }
                    }

                }
                break;
        }

        if (tmp) {
            this.clearFocuse();
            this.onFocuse(tmp);
        }
    },

    onFocuse(view) {
        view.onFocuse();
        this._selectView = view;
    },

    onKeyPressed(keyCode) {
        switch (keyCode) {
            case cc.KEY.up:
            case cc.KEY.left:
            case cc.KEY.right:
            case cc.KEY.down:
            case TVKey.right:
            case TVKey.up:
            case TVKey.left:
            case TVKey.down:
                this.choseFocuse(keyCode);
                break;
            case cc.KEY.enter:
            case TVKey.enter:
                if (this._selectView && this._selectView.node.active) {
                    this._selectView.onKeyEnter();
                }
                break;

            case cc.KEY.back:
                // case cc.KEY.backspace:
            case TVKey.back:
                {
                    if (this.backFun) {
                        this.backFun.emit([this.node, this.backFun.customEventData]);
                    }
                }
                break;
            case cc.KEY.menu:
            case TVKey.menu:
                {
                    if (this.menuFun) {
                        this.menuFun.emit([this.node, this.menuFun.customEventData]);
                    }
                }
                break;
        }

    },

    onLoad() {
        this._focuseArr = [];
        for (var i = 0; i < this.node.childrenCount; i++) {
            if (this.node.children[i].getComponent('KeyView')) {
                this._focuseArr.push(this.node.children[i].getComponent('KeyView'));
            }
        }
        if (this.autoRegistEvent) {
            cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, function (event) {
                event.stopPropagationImmediate();
                this.onKeyPressed(event.keyCode);
            }, this);
        }
    },



    start() {
        if (this._focuseArr == null)
            return;
        this._selectView = null;
        if (this._focuseArr.length > 0) {
            if (this.DefaultView != null && this.DefaultView.active == true) {
                this.onFocuse(this.DefaultView.getComponent('KeyView'));
            } else {
                for (var i = 0; i < this._focuseArr.length; i++) {
                    if (this._focuseArr[i].node.active == true) {
                        this.onFocuse(this._focuseArr[i]);
                        return;
                    }
                }
            }
        }
    },


    showDeafult() {
        if (this._focuseArr == null)
            return;
        this.clearFocuse();
        if (this._focuseArr.length > 0) {
            if (this.DefaultView != null && this.DefaultView.active == true) {
                this.onFocuse(this.DefaultView.getComponent('KeyView'));
                return;
            } else {
                for (var i = 0; i < this._focuseArr.length; i++) {
                    if (this._focuseArr[i].node.active == true) {
                        this.onFocuse(this._focuseArr[i]);
                        return;
                    }
                }
            }
        }
        this._selectView = null;
    }

    // update (dt) {},
});